package com.swit.mobile.xml;

import com.swit.mobile.jdf.Config;
import com.swit.mobile.jdf.Configuration;
import com.swit.mobile.jdf.ConfigurationException;
import com.swit.mobile.servlet.*;


import java.io.StringWriter;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.util.Hashtable;

public class SAXMessage {
	private static SAXMessage _instance;
	private String class_nm;
	public SAXMessage() throws Exception{
		class_nm = getClass().toString();
	}

	public static synchronized SAXMessage getInstance() throws Exception{
			if( _instance == null )  _instance = new SAXMessage();
			return _instance;
	}
	
    /**
      * @param args
      */
   public String convert(Hashtable getValue) throws Exception{
      DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();
      Document doc = parser.newDocument();
      
      Element root = doc.createElement("swit");
      doc.appendChild(root);
      
      Element version = doc.createElement("return");
      version.setAttribute("code", 	  (String)getValue.get("code"));
      version.setAttribute("message", (String)getValue.get("message"));
      
      root.appendChild(version);
      
      
	      
      
      TransformerFactory factory = TransformerFactory.newInstance();
      Transformer former = factory.newTransformer();
      former.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
      former.setOutputProperty(OutputKeys.INDENT, "yes");
      
      StringWriter sw = new StringWriter();
      StreamResult result = new StreamResult(sw);
      DOMSource source = new DOMSource(doc);
      former.transform(source, result);
      
      return sw.toString();
     }

}
